<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

namespace app\mobile\controller;

use app\common\controller\Users;
use think\Cookie;
use think\Controller;
use app\common\controller\Frontends;
use think\Session;
use think\Request;

/**
 * Description of User
 *
 * @author Administrator
 */
class User extends Frontends
{
    private $users;
    public function __construct(Request $request = null)
    {
        /* echo \config('view_replace_str.__ROOT__');
        // echo APP_PATH;
        echo "<hr>";
        echo \config('view_replace_str.__PUBLIC__');
        echo "<hr>";
        echo \config('view_replace_str.__CDN__');die; */
        parent::__construct($request);
        $this->users = new Users();
    }

    public function index()
    {
        $info = $this->users->getUserInfo($this->uid);
        $this->assign('info', $info);
        return $this->fetch();
    }

    public function login()
    {
        $url = $this->request->request('url', url('index'));
        if ($this->uid) {
            $this->success('您已经登录', $url);
        }
        if ($this->request->isPost()) {
            $data = $this->request->post();
            if (!captcha_check($data['wordcode'])) {
                ajax_return(0, '验证码错误');
            }
            $aa = $this->users->login($data);
            if ($aa) {
                ajax_return(1, '登录成功', $aa);
            } else {
                ajax_return(0, $this->users->errormsg);
            }
        } else {
            return $this->fetch();
        }
    }

    public function register()
    {
        $url = $this->request->request('url', url('user/index'));
        if ($this->uid) {
            $this->success('您已经登录', $url);
        }
        if ($this->request->isPost()) {
            $data = $this->request->post();
            $aa = $this->users->register($data);
            if ($aa) {
                ajax_return(1, '注册成功', $aa);
            } else {
                ajax_return(0, $this->users->errormsg, $aa);
            }
        }
        return $this->fetch();
    }

    public function getSms()
    {
        $data = $this->request->post();
        $data['mobile'] = '18338198234';
        if (!captcha_check($data['wordcode'])) {
            ajax_return(0, '验证码错误');
        }
        if ($this->users->sendSms($data['mobile'])) {
            ajax_return(1, '验证码发送成功');
        }
        ajax_return(0, $this->users->errormsg);
    }

    public function logout()
    {
        Cookie::delete('uid');
    }

    /* public function test(){
         //存session
         $data['sms'] = '1234';
         $data['time'] = time();
         Session::set('18338198234',json_encode($data));
         var_dump($data);

         $res = send_sms('12312312','18338198234');
         var_dump($res);
         var_dump(json_decode($res));
         var_dump(json_decode($res,true));
      }*/

    public function myInfo()
    {
        if ($this->request->isPost()) {
            $this->users->myInfoUpdate();
        }
        $info = $this->users->getUserInfo($this->uid);
        $this->assign('info', $info);
        return $this->fetch();
    }

    public function perfectInfo()
    {
        if ($this->request->isPost() && $this->uid) {
            $data = input('post.');
            $code = $this->users->perfectInfo($data, $this->uid);
            if ($code) {
                ajax_return(1, '完善信息成功');
            } else {
                ajax_return(-1, $this->users->errormsg);
            }
        }
        return $this->fetch();
    }

    public function protocolTwo()
    {
        return $this->fetch();
    }

    public function protocolOne()
    {
        return $this->fetch();
    }
    public function heardUpdate(){
        $res = $this->users->heardUpdate($this->uid);
        if($res){
            \ajax_return(1,'更改成功',$res);
        }
        \ajax_return(0,$this->users->errormsg);
    }
}
